//
//  SEAuthApi.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/18.
//

import Moya

enum SEAuthAPI {
    case checkRegister(phone: String, code: String)
    case register(authViewModel: SEAuthViewModel, userInfoModel: SEUserInfoModel)
    case login(account: String, code: String, sysCode: String = "XTBM-YKAPP", roleCode: String = "SHJS-YS", clientType: String = "KHDLX-APP", loginModel: Int = 2)
    case userInfo
    case editUserInfo(userInfoModel: SEUserInfoModel)
    case getMsg(phone: String)
    case bindPushAccount(devNum: String)
    case logout
}

extension SEAuthAPI: TargetType {
    var baseURL: URL {
        return URL(string: SEApiManager.baseUrl)!
    }

    var path: String {
        switch self {
        case .checkRegister: return "/smarteye/smarteye-base/user/check/register"
        case .register: return "/smarteye/smarteye-base/user/register"
        case .login: return "/ng/newfuture-auth/user/login"
        case .userInfo: return "/smarteye/smarteye-base/doctor/query/detail"
        case .editUserInfo: return "/smarteye/smarteye-base/doctor/change/info"
        case .getMsg: return "/ng/newfuture-msg/sms/vCode"
        case .bindPushAccount: return "/ng/newfuture-auth/pushAccount/bindPushAccount"
        case .logout: return "/ng/newfuture-auth/user/logout"
        }
    }

    var method: Moya.Method {
        return .post
    }

    var sampleData: Data {
        return "".data(using: .utf8)!
    }

    var task: Task {
        SELog("\(path) - \(parameters)")
        return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
    }

    var headers: [String : String]? {
        return SEApiManager.headers
    }

    private var parameters: [String: Any] {
        switch self {
        case let .checkRegister(phone, code):
            return ["phone": phone,
                    "code": code]
        case let .register(authViewModel, userInfoModel):
            return ["name": userInfoModel.name,
                    "idNumber": userInfoModel.idCard,
                    "phone": authViewModel.account,
                    "source": 1,
                    "code": authViewModel.code,
                    "hospitalId": userInfoModel.hospitalId,
                    "deptName": userInfoModel.deptName,
                    "standardId": userInfoModel.standardId,
                    "jobTitle": userInfoModel.jobTitleCode,
                    "introduction": userInfoModel.introduction,
                    "speciality": userInfoModel.speciality]
        case let .login(account, code, sysCode, roleCode, clientType, loginModel):
            return ["account": account,
                    "mobilePhoneCode": code,
                    "sysCode": sysCode,
                    "roleCode": roleCode,
                    "clientType": clientType,
                    "loginModel": loginModel]
        case .userInfo:
            return [:]
        case let .editUserInfo(userInfoModel):
            return ["id": userInfoModel.id,
                    "jobTitleCode": userInfoModel.jobTitleCode,
                    "introduction": userInfoModel.introduction,
                    "speciality": userInfoModel.speciality,
                    "hospitalId": userInfoModel.hospitalId,
                    "deptName": userInfoModel.deptName,
                    "standardId": userInfoModel.standardId]
        case let .getMsg(phone):
            return ["phone": phone]
        case let .bindPushAccount(devNum):
            return ["devNum": devNum,
                    "osType": "1",
                    "sysCode": "XTBM-YKAPP",
                    "clientType": "KHDLX-APP",
                    "puid": UserDefaults.userId]
        case .logout:
            return [:]
        }
    }
}
